package com.college.controller.user;

import com.college.common.pojo.ao.UserInfo;
import com.college.common.pojo.dto.userInfo.*;
import com.college.common.pojo.model.page.PageParamRequest;
import com.college.common.pojo.vo.userInfo.UserInfoLoginVO;
import com.college.common.response.CommonPage;
import com.college.common.response.CommonResult;
import com.college.common.utils.object.ObjectUtil;
import com.college.userInfo.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 小C工作室
 * @Date: 2021/9/24 22:58
 * @description: 操作日志后台管理
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户信息")
@Slf4j
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation(value = "查询用户信息列表")
    @GetMapping(value = "/query-list")
    public CommonResult<List<UserInfo>> queryList(QueryUserInfoDTO queryUserInfoDTO) {
        return CommonResult.success(userInfoService.queryList(queryUserInfoDTO));
    }

    @ApiOperation(value = "查询用户信息列表分页")
    @GetMapping(value = "/query-list-page")
    public CommonResult<CommonPage<UserInfo>> queryListPage(QueryUserInfoDTO queryUserInfoDTO , PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(userInfoService.queryListPage(queryUserInfoDTO,pageParamRequest)));
    }

    @ApiOperation(value = "新增用户信息")
    @PostMapping(value = "/add")
    public CommonResult<String> addUser(@RequestBody AddUserInfoDTO addUserInfoDTO) {
        return userInfoService.addUser(addUserInfoDTO);
    }

    @ApiOperation(value = "删除用户信息")
    @DeleteMapping(value = "/delete/{id}")
    public CommonResult<Boolean> deleteUser(@PathVariable Long id) {
        return CommonResult.success(userInfoService.removeById(id));
    }

    @ApiOperation(value = "批量删除用户信息")
    @DeleteMapping(value = "/batch-delete")
    public CommonResult<Boolean> deleteUser(@RequestBody List<Long> ids) {
        if (ObjectUtil.isNotEmpty(ids)) {
            return CommonResult.success(userInfoService.removeByIds(ids));
        }
        return CommonResult.success("参数错误");
    }

    @ApiOperation(value = "更新用户信息")
    @PutMapping(value = "/update")
    public CommonResult<String> updateUser(@RequestBody UpdateUserInfoDTO updateUserInfoDTO) {
        return userInfoService.updateUser(updateUserInfoDTO);
    }

    @ApiOperation(value = "用户注册")
    @PostMapping(value = "/register")
    public CommonResult<String> register(@Validated @RequestBody UserInfoRegisterDTO userInfoRegisterDTO) {
        return userInfoService.register(userInfoRegisterDTO);
    }

    @ApiOperation(value = "用户登录")
    @PostMapping(value = "/login")
    public CommonResult<UserInfoLoginVO> login(@Validated @RequestBody UserInfoLoginDTO userInfoLoginDTO) {
        return userInfoService.login(userInfoLoginDTO);
    }

    @ApiOperation(value = "更新密码")
    @PutMapping(value = "/update-login-info")
    public CommonResult<String> updateLoginInfo(@Validated @RequestBody UserInfoLoginDTO userInfoLoginDTO) {
        return userInfoService.updateLoginInfo(userInfoLoginDTO);
    }

}
